package classes.view.component
{
	import classes.controller.DAOSendFlash;
	import classes.controller.DAOSendJS;
	import classes.controller.EDataObject;
	import classes.controller.ESendJS;
	import classes.model.VOIsLocal;
	import classes.model.VOUserInfo;
	
	import com.d4p.custom.defaultButton.DefaultBtn;
	import com.d4p.util.MouseMove;
	import com.greensock.TweenMax;
	import com.greensock.easing.Cubic;
	import com.greensock.easing.Quart;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.external.ExternalInterface;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	public class PopupFriends extends Sprite
	{
		private var _currentType:Boolean;   // false : 친구 관리, true : 친구추가.
		
		private var _selectChange:MovieClip;
		private var _selectAdd:MouseMove;
		private var _selectList:MouseMove;
		
		private var _listBtn:Sprite;
		private var _addBtn:Sprite;
		
		private var _top:MouseMove;
		
		private var _listContainer:PopupFriend_ListContainer;
		
		
		
		private var _close:DefaultBtn;
		private var _addTop:PopupFriend_Add;
		
		public function PopupFriends()
		{
			super();
			setLayOut();
			initListener();
			defaultSetting();
			
		}
		private function data_send():void
		{
			//popup은 화면에 붙이고나서 이벤트를 발생시킴.  
			var obj:Object = obj ={
				"data":[
					{
						"id":VOUserInfo.getDataInstance().data.id
					}
				]
			}
			DAOSendFlash.getLoadInstance().setData("getFriendList",obj);
		}
		
		
		private function setLayOut():void
		{
			this._selectChange	=   this.getChildByName('titleSelect_mc')			as MovieClip;
			this._selectAdd		= 	this._selectChange.getChildByName('add_mc')		as MouseMove;
			this._selectList	= 	this._selectChange.getChildByName('list_mc')	as MouseMove;
			this._addTop		=	this.getChildByName('addtop_mc')				as PopupFriend_Add;
			this._top			=   this.getChildByName('top_mc')					as MouseMove;
			this._close			=	this.getChildByName('close_mc')					as DefaultBtn;
			this._listContainer = 	this.getChildByName('list_container')			as PopupFriend_ListContainer;
			
			this._listBtn		= 	this.getChildByName('btn_0')					as Sprite;
			this._addBtn		= 	this.getChildByName('btn_1')					as Sprite;
			
		}
		private function initListener():void
		{
			this._close.addEventListener(MouseEvent.CLICK,			closeClick);
			
			this._addBtn.addEventListener(MouseEvent.CLICK,			onMouseClickHandler);
			this._listBtn.addEventListener(MouseEvent.CLICK,		onMouseClickHandler);
			this._addBtn.addEventListener(MouseEvent.ROLL_OVER,		onRollOverHandler);
			this._listBtn.addEventListener(MouseEvent.ROLL_OVER,	onRollOverHandler);
			
			this._listContainer.addEventListener("withGame",		withGameSend);
		}
		private function onMouseClickHandler(e:MouseEvent):void
		{
			e.currentTarget.removeEventListener(MouseEvent.ROLL_OUT,	onRollOutHandler);
			
			if(e.currentTarget == _addBtn){
				typeSelectChange = false;
			}else{
				typeSelectChange = true;
			}
		}
		private function withGameSend(e:Event):void
		{
			dispatchEvent(new EDataObject(["withGame"], EDataObject.EVENT_DATAOBJECT));
		}
		
		private function set typeSelectChange($type:Boolean):void
		{
			if($type){
				//친구관리 on.
				_listContainer.viewType 			= 0;
				_listContainer.y 					= 249;
				_listContainer.pagingY 				= 289;
				_listContainer.viewNum 				= 7;
				_listContainer.resetList();
				
				this._listBtn.mouseChildren 		= false;
				this._listBtn.mouseEnabled  		= false;
				this._addBtn.mouseChildren  		= true;
				this._addBtn.mouseEnabled   		= true;
				
				this._selectList.playFrame('over');
				this._selectAdd.playFrame('out');
				
				this._top.playFrame('out');
				
				this._addTop.alertOff();
				TweenMax.to(this._addTop, 0.3, {y:207,  autoAlpha:0, ease:Cubic.easeOut});
				
				//friendList 다시 달래기. 
				var obj:Object = {
									"data":[
										{
											"id":VOUserInfo.getDataInstance().data.id
										}
									]
								}
				DAOSendFlash.getLoadInstance().setData("getFriendList",obj);
			}else{
				//친구추가 on.
				_listContainer.viewType 			= 1;
				_listContainer.y 					= 316;
				_listContainer.pagingY 				= 222;
				_listContainer.viewNum 				= 5;
				_listContainer.resetList();
				
				this._listBtn.mouseChildren 		= true;
				this._listBtn.mouseEnabled  		= true;
				this._addBtn.mouseChildren  		= false;
				this._addBtn.mouseEnabled   		= false;
				
				this._selectAdd.playFrame('over');
				this._selectList.playFrame('out');
				
				this._top.playFrame('in');
				this._addTop.y = 207;
				
				TweenMax.to(this._addTop, 0.3, {y:212, autoAlpha:1, ease:Cubic.easeOut});
				
			}
			_currentType =!_currentType;
		}
		
		private function dataListener():void
		{
			DAOSendJS.getLoadInstance().addEventListener(ESendJS.EVENT_FRIENDLIST, 		onFriendListLoadComplete);
		}
		
		private function onRollOverHandler(e:MouseEvent):void
		{
			if(e.currentTarget == _addBtn){
				_selectAdd.playFrame('over');
				_selectAdd.addEventListener(MouseEvent.ROLL_OUT,	onRollOutHandler);
			}else{
				_selectList.playFrame('over');
				_selectList.addEventListener(MouseEvent.ROLL_OUT,	onRollOutHandler);
			}
			
		}
		private function onRollOutHandler(e:MouseEvent):void
		{
			e.currentTarget.removeEventListener(MouseEvent.ROLL_OUT,	onRollOutHandler);
			if(e.currentTarget == _addBtn){
				_selectAdd.playFrame('out');
				
			}else{
				_selectList.playFrame('out');
				
			}
		}
		
		private function closeClick(e:MouseEvent):void 
		{
			DAOSendJS.getLoadInstance().removeEventListener(ESendJS.EVENT_FRIENDLIST, 		onFriendListLoadComplete);
			this.dispatchEvent(new EDataObject(["close"], 								EDataObject.EVENT_DATAOBJECT));
		}
		
		private function defaultSetting():void
		{
			this._listBtn.buttonMode 		= true;
			this._addBtn.buttonMode 		= true;
			
			//this._selectChange.buttonMode = true;
			this._selectAdd.stop();
			
			_listContainer.y = 249;
			_listContainer.viewNum = 7;
			_listContainer.resetList();
			this._selectList.gotoAndStop('over');
			
			
			this._selectList.mouseChildren = false;
			this._selectList.mouseEnabled  = false;
			this._selectAdd.mouseChildren  = false;
			this._selectAdd.mouseEnabled   = false;
			
			this._listBtn.mouseChildren = false;
			this._listBtn.mouseEnabled  = false;
			this._addBtn.mouseChildren  = true;
			this._addBtn.mouseEnabled   = true;
			
			
			this._top.stop();
		}
		
		public function data_init():void
		{
			if(VOIsLocal.getLocalInstance().isLocal == "FILE"){
				var loader:URLLoader = new URLLoader();
				loader.addEventListener( Event.COMPLETE,	onDataLoaded);
				try {
					loader.load( new URLRequest('sample/PopupFriendList.json'));
				}catch ( error:Error ) {
					trace( "Unable to load requested document." );
				}
			}else{
				//real.
				dataListener();
				data_send();
				
			}
		}
		
		
		private function onDataLoaded(e:Event):void
		{
			var jsonParsed:*  = JSON.parse(e.target.data);
			loadFriendList 	= jsonParsed.data;
			
			
		}
		private function onFriendListLoadComplete(e:ESendJS):void
		{
			loadFriendList = e._obj;
		}
		
		private function set loadFriendList($obj:Object):void 
		{
		
			
			_listContainer.resetList();
			_listContainer.listOn = $obj;
			_listContainer.pageOn = 1;
		}
	}
}